/*
# File    :   spi.c
# Author  :   SCHIPS
# Mail    :   schips@dingtalk.com
# Date    :   Tue, Apr 30, 2019  4:04:21 PM
# Note    :   SPI with R/W SD card demo based on C51
*/

#include <reg52.h>
#include <intrins.h>
#include <stdio.h>
#include <9325TP.h>

#include "sd_card.h"
//=============================================================
//定义SD卡需要的4根信号线
sbit SPI_CLK = P1^1;
sbit SPI_DI  = P1^2;
sbit SPI_DO  = P1^0;
sbit SPI_CS  = P1^3;
//===========================================================
//定义按键端口
sbit KEY = P3^2;
//===========================================================
//定义512字节缓冲区，注意需要使用 xdata关键字
unsigned char xdata DATA[75]={0};
unsigned char xdata DATA1[75]={0};


#if 1
// for demo
//主程序 
int main(void)
{

    unsigned long AddTemp=262144;//SD卡地址第一个数据物理地址初始值，可以用winhex查看，这里是512扇区，512x512=262144，根据实际SD卡内容更改
    unsigned char i;
    unsigned char *p;
       CS = 1;
     delayms(5);
     RES = 0;
     delayms(5);
     RES = 1;
     delayms(5);
     SdInit();         //SD卡初始化
     Com_Init();
     for(i = 0; i < 75; i++)
     {
        DATA1[i] = i;
     }   

    SdWriteBlock(DATA1, AddTemp, 75);
    SdReadBlock(DATA, AddTemp, 75);
    p = DATA;
    while(1)
    {
        for(i = 0;i < 75;i++)
        {
            SBUF = *p + 48;
            while(!TI)                   //如果发送完毕，硬件会置位TI
            {
                _nop_(); 
            }
            p++;
            TI = 0;                  //TI清零
            delayms(500);
        }
    }
}
#endif
